package com.ecommerce.controller;

import com.ecommerce.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/orders")
@CrossOrigin(origins = "*")
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 获取用户订单列表
    @GetMapping
    public ResponseEntity<Map<String, Object>> getUserOrders(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String status) {
        Map<String, Object> result = orderService.getUserOrders(userId, page, size, status);
        return ResponseEntity.ok(result);
    }

    // 获取订单详情
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getOrderDetail(
            @RequestParam Long userId,
            @PathVariable Long id) {
        Map<String, Object> result = orderService.getOrderDetail(userId, id);
        return ResponseEntity.ok(result);
    }

    // 创建订单
    @PostMapping
    public ResponseEntity<Map<String, Object>> createOrder(
            @RequestParam Long userId,
            @RequestBody Map<String, Object> orderData) {
        Map<String, Object> result = orderService.createOrder(userId, orderData);
        return ResponseEntity.ok(result);
    }

    // 根据购物车创建订单（用于支付）
    @PostMapping("/create-from-cart")
    public ResponseEntity<Map<String, Object>> createOrderFromCart(
            @RequestParam Long userId,
            @RequestBody Map<String, Object> orderData) {
        Map<String, Object> result = orderService.createOrderFromCart(userId, orderData);
        return ResponseEntity.ok(result);
    }

    // 取消订单
    @PutMapping("/{id}/cancel")
    public ResponseEntity<Map<String, Object>> cancelOrder(
            @RequestParam Long userId,
            @PathVariable Long id) {
        Map<String, Object> result = orderService.cancelOrder(userId, id);
        return ResponseEntity.ok(result);
    }
}